// ====================DataManager====================

/**
 * 制作职业数据
 */
DataManager.makeClassData = function () {
    for (const data of $dataClasses) {
        if(!data) continue;
        let cards = [];
        const obj = data.meta["卡组"];
        if(typeof obj === "string") {
            cards = obj.split(",").map(id => Number(id.trim()) || 1);
        }
        data.cards = cards;
    }
}

/**
 * 制作card数据
 */
DataManager.makeCardData = function () {
    window.$dataCards = [];
    // 通过rm技能数据转化
    for (const data of $dataSkills) {
        const card = this.makeCardDataBySkill(data);
        window.$dataCards.push(card);
    }
}

/**
 * 通过rm技能数据转化为卡牌数据
 * @param data
 */
DataManager.makeCardDataBySkill = function (data) {
    if (!data || data.name === "") return null;
    const id = data.id;
    const skillId = id;
    const name = data.name;
    const cost = data.mpCost;
    const damage = data.damage.formula;
    const note = data.note;
    const card = {id, skillId, name, cost, damage};
    this.extractMetaData(card, data);
    this.analyzeCardSeq(card, note);
    return card;
}

DataManager.extractMetaData = function (card, data) {
    // 处理图像
    const image = data.meta["卡牌图像"];
    card.image = image || "";
    // 处理护甲
    const armor = Number(data.meta["卡牌护甲"]);
    card.armor = armor || 0;
    data.armor = armor || 0;
    // 处理类型
    const type = data.meta["卡牌类型"];
    card.type = !!type ? type.split(",").map(type => type.trim()) : [];
    // 处理序列
    // const actionSeq = data.meta.ACTION_SEQ;
    // card.actionSeq = !!actionSeq ? actionSeq.split(",").map((action) => {
    //     return action.trim();
    // }) : ["技能"];
    // 处理条件
    const requirement = data.meta["卡牌条件"];
    card.requirement = requirement;
}

DataManager.analyzeCardSeq = function (card, note) {
    const regex = /<卡牌效果>([\s\S]*?)<\/卡牌效果>/i;
    const match = note.match(regex);
    const seq = match ? match[1].split("\n") : null;
    if (!seq) {
        card.actionSeq = [{name: "行动", args: []}];
        return;
    }
    const data = [];
    for (const line of seq) {
        if (!line) continue;
        const obj = line.split(":");
        const name = obj[0];
        let args;
        if (obj[1]) {
            args = obj[1].split(",").map(arg => arg.trim());
        } else {
            args = [];
        }
        data.push({name, args});
    }
    card.actionSeq = data;
}